package cn.wolfcode.shop.cloud.configuration;

import cn.wolfcode.shop.cloud.key.RedisProperties;
import cn.wolfcode.shop.cloud.service.RedisService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnProperty(prefix = "redis",name = "host")
public class RedisAutoConfiguration {

    //连接池
    @Bean
    public JedisPool jedisPool(RedisProperties redisProperties){
        //连接池的配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //jedis池配置设置最大空闲
        jedisPoolConfig.setMaxIdle(redisProperties.getPoolMaxIdle());
        //jedis池配置设置最大总计
        jedisPoolConfig.setMaxTotal(redisProperties.getPoolMaxTotal());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getPoolMaxWait());
        return new JedisPool(jedisPoolConfig,redisProperties.getHost(),redisProperties.getPort(),redisProperties.getTimeout(),redisProperties.getPassword());
    }

    @Bean
    public RedisService redisService(){
        return new RedisService();
    }

}
